import { createRouter, createWebHistory } from 'vue-router'


const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path:'/',
      redirect:'/noteClass'
    },
    {
      path:'/login',
      name:'login',
      component:()=>import('@/views/Login.vue'),
      meta:{
        title:'登录'
      }
    },
    {
      path:'/register',
      name:'register',
      component:()=>import('@/views/Register.vue'),
      meta:{
        title:'注册'
      }
    },
    {
      path:'/noteClass',
      name:'noteClass',
      component:()=>import('@/views/noteClass.vue'),
      meta:{
        title:'笔记分类'
      }
    },
    {
      path:'/noteList',
      name:'noteList',
      component:()=>import('@/views/NoteList.vue'),
      meta:{
        title:'日记分类'
      }
    },
    {
      path:'/noteDetail',
      name:'noteDetail',
      component:()=>import('@/views/noteDetail.vue'),
      meta:{
        title:'日记日记详情'
      }
    },
    {
      path:'/notePublish',
      name:'notePublish',
      component:()=>import('@/views/NotePublish.vue'),
      meta:{
        title:'日记日记详情'
      }
    }



  ]
})
//全局路由守卫
const whitePath=['/login','/register']
  router.beforeEach((to,from,next)=>{
   // console.log(to, from);
    document.title=to.meta.title
    if(!whitePath.includes(to.path)){
      //判断本地有无用户数据
      if(!localStorage.getItem('userInfo')){
        router.push('/login')
        return
      }
      next()
      return
    }
    next()
  })

export default router
